package com.kangyi.admin.controller;

import com.kangyi.admin.service.NursingHomeEventService;
import com.kangyi.dto.AddNursingHomeEventDTO;
import com.kangyi.dto.EditNursingHomeEventDTO;
import com.kangyi.dto.NursingHomeEventPageQueryDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.NursingHomeEventVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/event")
@Slf4j
@Api(tags = "养老院-精彩活动相关接口")
public class NursingHomeEventController {

    @Autowired
    private NursingHomeEventService nursingHomeEventService;

    @GetMapping("/page")
    @ApiOperation("精彩活动分页查询")
    public Result<PageResult> pageQuery(NursingHomeEventPageQueryDTO nursingHomeEventPageQueryDTO){
        log.info("精彩活动分页查询:{}",nursingHomeEventPageQueryDTO);
        try {
            PageResult pageResult = nursingHomeEventService.pageQuery(nursingHomeEventPageQueryDTO);
            return Result.success(pageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("新增精彩活动")
    @CacheEvict(cacheNames = "nursingHomeEvent_", allEntries = true)
    public Result addEvent(@RequestBody AddNursingHomeEventDTO addNursingHomeEventDTO){
        log.info("新增精彩活动:{}",addNursingHomeEventDTO);
        try {
            nursingHomeEventService.addEvent(addNursingHomeEventDTO);
            return Result.success("新增成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("查询精彩活动详情")
//    @Cacheable(cacheNames = "nursingHomeEvent_", key = "'adminGet_'+#id")
    public Result<NursingHomeEventVO> getEvent(@PathVariable Integer id){
        log.info("查询精彩活动详情：{}",id);
        try {
            NursingHomeEventVO nursingHomeEventVO = nursingHomeEventService.getEvent(id);
            return Result.success(nursingHomeEventVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/edit")
    @ApiOperation("修改精彩活动")
    @CacheEvict(cacheNames = "nursingHomeEvent_", allEntries = true)
    public Result editEvent(@RequestBody EditNursingHomeEventDTO editNursingHomeEventDTO){
        log.info("修改精彩活动：{}",editNursingHomeEventDTO);
        try {
            nursingHomeEventService.editEvent(editNursingHomeEventDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除精彩活动")
    @CacheEvict(cacheNames = "nursingHomeEvent_", allEntries = true)
    public Result deleteEvent(@RequestParam List<Integer> ids){
        log.info("批量删除精彩活动");
        try {
            nursingHomeEventService.deleteEvent(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
